package net.oschina.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.MApp;
import net.oschina.app.R;
import net.oschina.app.bean.Blog;
import net.oschina.app.bean.BlogList;
import net.oschina.app.interf.OnRecycleViewItemClickListener;
import net.oschina.app.ui.adapter.BlogAdapter;
import net.oschina.app.ui.view.BlogWebActivity;
import net.oschina.app.util.Md5Utils;
import net.oschina.app.util.MyHttpUtils;
import net.oschina.app.util.XmlUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/4/5.
 * ?
 */

public class BlogActivity extends AppCompatActivity{
    private Toolbar mToolbar;
    private XRecyclerView mXRecyclerView;
    private BlogAdapter mBlogAdapter;
    private List<Blog> mBloglist;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_blog);
        mToolbar = (Toolbar) findViewById(R.id.tb_my_blog);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.actionbar_back_icon_normal);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mXRecyclerView = (XRecyclerView) findViewById(R.id.rv_my_blog);
        LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
        mXRecyclerView.setLayoutManager(manager);
        View view = View.inflate(getApplicationContext(), R.layout.xrecyclerview_footer, null);
        mXRecyclerView.setFootView(view);

        String url = "http://10.0.2.2:8080/oschina/list/userblog_list/page0.xml";
        String key= Md5Utils.md5(url);
        String result = MApp.mCacheUtils.getCache(key);
        if (result == null){
            Toast.makeText(this, "联网获取数据", Toast.LENGTH_SHORT).show();
            //联网获取数据
            MyHttpUtils load = new MyHttpUtils();
            load.getDate(url, BlogList.class, new MyHttpUtils.OngetDatesResult() {
                @Override
                public void Onsucessful(Object bean) {

                    BlogList list = (BlogList) bean;
                    mBloglist = list.getBloglist();
                   runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           mBlogAdapter = new BlogAdapter(getApplicationContext(), mBloglist);
                           mXRecyclerView.setAdapter(mBlogAdapter);
                           inteEvent();
                       }
                   });
                }
                @Override
                public void Onfail() {
                }
            });

        }else {
             Toast.makeText(this, "内存或硬盘获取数据", Toast.LENGTH_SHORT).show();
            BlogList blogList = XmlUtils.toBean(BlogList.class, result.getBytes());
            mBloglist = blogList.getList();
            mBlogAdapter = new BlogAdapter(getApplicationContext(), mBloglist);
            mXRecyclerView.setAdapter(mBlogAdapter);
            inteEvent();
        }


    }

    private void inteEvent() {
        mBlogAdapter.setOnItemClickListener(new OnRecycleViewItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(BlogActivity.this, BlogWebActivity.class);
                // Toast.makeText(BlogActivity.this, "fff"+position, Toast.LENGTH_SHORT).show();
                /*
                * 需要注意这里的position是有误差的，差值为1，所以传递的url的position需要减 1
                */
                intent.putExtra("url",mBloglist.get(position-1).getUrl());
                startActivity(intent);
            }
        });
    }


}
